//
//  AbstractWebCutoutDataView.m
//  MIS Cloud
//
//  Created by goldeney on 14-4-1.
//  Copyright (c) 2014年 goldeneye. All rights reserved.
//

#import "AbstractWebCutoutDataView.h"
#import "AnalysisURLCiphertext.h"

@interface AbstractWebCutoutDataView()

@property (nonatomic, strong) NSString *enterString;
@property (nonatomic, strong) NSString *functionString;

@end

@implementation AbstractWebCutoutDataView
@synthesize delegate = _delegate;
@synthesize enterString = _enterString;
@synthesize functionString = _functionString;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

#pragma mark A-->B页面传递的数据
- (void)setEnterFunction:(NSString *)functionStr parameter:(NSString *)enterString
{
    self.enterString = enterString;
    self.functionString = functionStr;
}

- (void)dealloc
{
    _delegate = nil;
    _enterString = nil;
}

- (BOOL)operationTypeHandle:(NSString*)dataString
{
    //解析数据后得到数据结果
    NSDictionary *webViewDic = [AnalysisURLCiphertext analysisURLString:dataString];
    
    if (webViewDic) {
        NSString *type = [webViewDic objectForKey:CSMonitorNodeDataKeyType];
        if ([type isEqualToString:CSMonitorNodeOrderTypeReturn]) {//返回类型
            if (_delegate && [_delegate respondsToSelector:@selector(returnViewControllerData:)])
                [_delegate returnViewControllerData:webViewDic];
            return NO;
            
        } else if ([type isEqualToString:CSMonitorNodeOrderTypeJump]) {//跳转类型
            NSString *enterData = [self getEnterAToBWindowsData:webViewDic];//获取A页面数据，用于传递到B页面
            if (_delegate && [_delegate respondsToSelector:@selector(jumpNewViewControllerData: enrteObject:)])
                [_delegate jumpNewViewControllerData:webViewDic enrteObject:enterData];
            return NO;
            
        } else if ([type isEqualToString:CSMonitorNodeOrderTypeDown]) {//下载类型
            [self getDownFileWindowsData:webViewDic];
            return NO;
            
        } else if ([type isEqualToString:CSMonitorNodeOrderTypeUpload]) {//上传类型
            [self getUpFileWindowsData:webViewDic];
            return NO;
            
        } else if ([type isEqualToString:CSMonitorNodeOrderTreeMenu]) {//选择对象（树形结构）
            [self getTreeMenuWindowsData:webViewDic];
            return NO;
            
        } else {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"不支持的功能，请升级软件" delegate:self
                                                cancelButtonTitle:@"确定" otherButtonTitles:nil];
            alert.alertViewStyle=UIAlertViewStyleDefault;
            [alert show];
            return YES;
        }
    }
    
    return YES;
}

#pragma mark -
#pragma mark UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //UIWebViewNavigationTypeLinkClicked，用户触击了一个链接。
    //UIWebViewNavigationTypeFormSubmitted，用户提交了一个表单。
    //UIWebViewNavigationTypeBackForward，用户触击前进或返回按钮。
    //UIWebViewNavigationTypeReload，用户触击重新加载的按钮。
    //UIWebViewNavigationTypeFormResubmitted，用户重复提交表单
    //UIWebViewNavigationTypeOther，发生其它行为。
    
    //未点击连接不触发事件
    //if (navigationType != UIWebViewNavigationTypeLinkClicked) return YES;
    NSString *requestString = [[request URL] absoluteString];
    BOOL isRequest = [self operationTypeHandle:requestString];
    
    return isRequest;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [super webViewDidStartLoad:webView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [super webViewDidFinishLoad:webView];
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout = 'none';"];
    [self enterAToBWindowsFunction:_functionString parameter:_enterString];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [super webView:webView didFailLoadWithError:error];
}

#pragma mark -
#pragma mark A到B页面数据传递

- (NSString*)getEnterAToBWindowsData:(NSDictionary*)webViewDic
{
    //获取到A页面传递过来的参数
    NSDictionary *enterData = [webViewDic objectForKey:CSMonitorNodeDataKeyEnter];
    NSString *functionAData = [enterData objectForKey:CSMonitorNodeOrderEnterFunctionA];
    
    //判断functionB数据是否存在
    if (functionAData && ![functionAData isEqualToString:@""]) {
        NSString *functionA = [NSString stringWithFormat:@"%@();",functionAData];
        NSString *scriptString = [self stringByEvaluatingJavaScriptFromString:functionA];
        return scriptString;
    }
    
    return nil;
}

- (void)enterAToBWindowsFunction:(NSString*)function parameter:(NSString*)functionAData
{
    //判断functionB数据是否存在
    if (function && ![function isEqualToString:@""]) {
        NSString *functionB = nil;
        if (functionAData && ![functionAData isEqualToString:@""]){
            functionB = [NSString stringWithFormat:@"%@(\"%@\");",function,functionAData];
        } else {
            functionB = [NSString stringWithFormat:@"%@();",function];
        }
        
        [self stringByEvaluatingJavaScriptFromString:functionB];
    }
    
}

#pragma mark -
#pragma mark 下载文件处理
- (void)getDownFileWindowsData:(NSDictionary*)webViewDic
{
    //获取文件名和下载路径
    NSDictionary *downData = [webViewDic objectForKey:CSMonitorNodeDataKeyDown];
    NSString *fileName = [downData objectForKey:CSMonitorNodeOrderDownFileName];
    NSString *filePath = [downData objectForKey:CSMonitorNodeOrderDownFilePath];
    
    if (_delegate && [_delegate respondsToSelector:@selector(downViewControllerFileName:filePath:)])
        [_delegate downViewControllerFileName:fileName filePath:filePath];
}

#pragma mark -
#pragma mark 上传文件处理
- (void)getUpFileWindowsData:(NSDictionary*)webViewDic
{
    //获取文件名和下载路径
    NSDictionary *uplaodData = [webViewDic objectForKey:CSMonitorNodeDataKeyUpload];
    if (_delegate && [_delegate respondsToSelector:@selector(upViewControllerData:)])
        [_delegate upViewControllerData:uplaodData];
}

#pragma mark -
#pragma mark 组织菜单（树形结构）
- (void)getTreeMenuWindowsData:(NSDictionary*)webViewDic
{
    //获取文件名和下载路径
    if (_delegate && [_delegate respondsToSelector:@selector(treeMenuViewControllerData:)])
        [_delegate treeMenuViewControllerData:webViewDic];
}

@end
